/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.statement;


import dex.compiler.model.base.Place;
import dex.compiler.model.expression.Expression;
import dex.compiler.model.type.TypeNode;


/**
 * A local variable declaration, such as <code>uint32 x</code>.
 */
public class Declaration extends Statement {
	
	
	/**
	 * The type of the variable being declared.
	 */
	private TypeNode type;
	
	
	/**
	 * The identifier of the variable being declared.
	 */
	private String id;
	
	
	/**
	 * The optional initializer for the declaration.
	 * May be null if the variable is not to be initialized immediately.
	 */
	private Expression initializer;
	

	/**
	 * Constructs a new declaration.
	 * 
	 * @param place  the place where the declaration originated
	 * @param type  the type of the variable being declared
	 * @param id  the identifier of the variable being declared
	 * @param initializer  the optional initializer for the new variable
	 */
	public Declaration(Place place, TypeNode type, String id, Expression initializer) {
		super(place);
		if (type == null) {
			throw new IllegalArgumentException();
		}
		if (id == null) {
			throw new IllegalArgumentException();
		}
		this.type = type;
		this.id = id;
		this.initializer = initializer;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(type).append(' ').append(id);
		if (initializer != null) {
			sb.append(" = ").append(initializer);
		}
		return sb.toString();
	}

	
	/**
	 * Returns the identifier for the variable being declared.
	 * 
	 * @return  the variable's identifier
	 */
	public String getIdentifier() {
		return id;
	}
	
	
	/**
	 * Returns the type of the variable being declared.
	 * 
	 * @return  the variable's type
	 */
	public TypeNode getType() {
		return type;
	}
	
	
	/**
	 * Returns the optional initializer of the variable being declared.
	 * If present, the variable will be assigned the result of the expression.
	 * If null, no such assignment occurs.
	 * 
	 * @return  the initializer, or null if there is no initializer
	 */
	public Expression getInitializer() {
		return initializer;
	}


	@Override
	public boolean terminates() {
		return false;
	}
}
